package com.b2b.controller;


import com.alibaba.fastjson.JSONObject;
import com.b2b.model.common.CategoryHot;
import com.b2b.model.common.ResponseBean;
import com.b2b.service.impl.comparator.CategoryHotComparator;
import com.b2b.util.GoodsUtil;
import com.b2b.util.JedisUtil;
import com.b2b.util.OrdersUtil;
import com.b2b.util.common.StatisticUtil;

import com.b2b.util.common.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import redis.clients.jedis.Jedis;

import java.util.*;

@RestController
@CrossOrigin
@RequestMapping("/statistic")
public class StatisticController {

    @Autowired
    OrdersUtil ordersUtil;

    @Autowired
    GoodsUtil goodsUtil;

    @GetMapping("/access")
    public ResponseBean access() {

        int year = TimeUtil.getYear();
        int month = TimeUtil.getMonth();
        int dayOfThisMonth = TimeUtil.dayOfThisMonth();
        List<String> dateList = new ArrayList<>();
        List<String> dataList = new ArrayList<>();
        if (dayOfThisMonth < 30) {
            if (month > 1) {
                int daysOfLastMonth = TimeUtil.daysOfLastMonth();
                int lastMonth = month - 1;
                int start = daysOfLastMonth + dayOfThisMonth - 29;
                Jedis jedis = JedisUtil.getJedis();
                assert jedis != null;
                String yearStr = String.valueOf(year);
                String YM = yearStr + String.format("%02d", lastMonth);
                for (; start <= daysOfLastMonth; start++) {
                    String str = YM + "-" + String.format("%02d", start);
                    dateList.add(str);
                    dataList.add(jedis.get(str));
                }
                YM = yearStr + String.format("%02d", month);
                for (start = 1; start <= dayOfThisMonth; start++) {
                    String str = YM + "-" + String.format("%02d", start);
                    dateList.add(str);
                    dataList.add(jedis.get(str));
                }
            } else {
                int start = dayOfThisMonth + 2;
                Jedis jedis = JedisUtil.getJedis();
                assert jedis != null;
                String yearStr = String.valueOf(year - 1);
                String YM = yearStr + 12;
                for (; start <= 31; start++) {
                    String str = YM + "-" + String.format("%02d", start);
                    dateList.add(str);
                    dataList.add(jedis.get(str));
                }
                yearStr = String.valueOf(year);
                YM = yearStr + month;
                for (start = 1; start <= dayOfThisMonth; start++) {
                    String str = YM + "-" + String.format("%02d", start);
                    dateList.add(str);
                    dataList.add(jedis.get(str));
                }
            }
        } else {
            int start = dayOfThisMonth - 29;
            Jedis jedis = JedisUtil.getJedis();
            assert jedis != null;
            String yearStr = String.valueOf(year);
            String YM = yearStr + String.format("%02d", month);
            for (; start <= dayOfThisMonth; start++) {
                String str = YM + "-" + String.format("%02d", start);
                dateList.add(str);
                dataList.add(jedis.get(str));
            }
        }
        Map<String, Object> result = new HashMap<>();
        result.put("dateList", dateList);
        result.put("dataList", dataList);

        return new ResponseBean(HttpStatus.OK.value(), "查询成功", result);
    }

    @GetMapping("/categoryHot")
    public ResponseBean categoryHot(){
        Map<String, Object> categoryHotMap = JSONObject.parseObject(JedisUtil.getJson("categoryHot")
                .replace('=', ':'));
        System.out.println(categoryHotMap);
        List<CategoryHot> categoryHotList = new ArrayList<>();
        for (Map.Entry<String, Object> entry: categoryHotMap.entrySet()
             ) {
            CategoryHot categoryHot = new CategoryHot();
            categoryHot.setCategoryName(entry.getKey());
            categoryHot.setSoldTimes(Integer.parseInt(entry.getValue().toString()));
            categoryHotList.add(categoryHot);
        }
        CategoryHot[] categoryHots = new CategoryHot[categoryHotList.size()];
        categoryHotList.toArray(categoryHots);
        Arrays.sort(categoryHots, new CategoryHotComparator());
        categoryHotList = Arrays.asList(categoryHots);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功", categoryHotList);
    }


    @GetMapping("/sellerIndex")
    public ResponseBean sellerIndex(@RequestParam Integer sellerId){
        int dayOfThisMonth = TimeUtil.dayOfThisMonth();
        int month = TimeUtil.getMonth();
        int year = TimeUtil.getYear();
        String base = "salesSum";
        List<String> dateList = new ArrayList<>();
        List<String> dataList = new ArrayList<>();
        if(dayOfThisMonth  < 7) {
            if(month > 1) {
                int daysOfLastMonth = TimeUtil.daysOfLastMonth();
                int lastMonth = month - 1;
                int start = daysOfLastMonth + dayOfThisMonth - 6;
                Jedis jedis = JedisUtil.getJedis();
                assert jedis != null;
                String yearStr = String.valueOf(year);
                String YM = base + yearStr + String.format("-%2d-", lastMonth);

                for (; start <= daysOfLastMonth; start++) {
                    String str = YM + String.format("%02d", start);
                    dateList.add(str.substring(8));
                    dataList.add(jedis.get(str));
                }
                YM = base + yearStr + String.format("-%2d-", month);
                for (start = 1; start <= dayOfThisMonth; start++) {
                    String str = YM + String.format("%02d", start);
                    dateList.add(str.substring(8));
                    dataList.add(jedis.get(str));
                }
            }else {
                int start = 25 + dayOfThisMonth;
                String yearStr = String.valueOf(year);
                String YM = base + yearStr + "-12-";
                Jedis jedis = JedisUtil.getJedis();
                assert jedis != null;
                for (; start <= 31; start++) {
                    String str = YM + String.format("%02d", start);
                    dateList.add(str.substring(8));
                    dataList.add(jedis.get(str));
                }
                YM = base + (year - 1) + "-01-";
                for (start = 1; start <= dayOfThisMonth; start++) {
                    String str = YM + String.format("%02d", start);
                    dateList.add(str.substring(8));
                    dataList.add(jedis.get(str));
                }
            }
        }else {
            int start = dayOfThisMonth - 6;
            String yearStr = String.valueOf(year);
            String YM = base + yearStr + String.format("-%2d-", month);
            Jedis jedis = JedisUtil.getJedis();
            assert jedis != null;
            for (; start <= dayOfThisMonth; start++){
                String str = YM + String.format("%02d", start);
                dateList.add(str.substring(8));
                dataList.add(jedis.get(str));
            }
        }

        Map<String, Object> result = new HashMap<>();
        result.put("salesSumToday", JedisUtil.getJson(StatisticUtil.getSalesSumKey(sellerId)));
        result.put("ordersToHandle", ordersUtil.getNumOfOrdersToDeliver(sellerId));
        result.put("numOfGoods", goodsUtil.getNumOfGoods(sellerId));
        result.put("dateList", dateList);
        result.put("dataList", dataList);

        return new ResponseBean(HttpStatus.OK.value(), "查询成功", result);
    }

    @GetMapping("/buyerIndex")
    public ResponseBean buyerIndex(@RequestParam Integer buyerId){
        int ordersToPay = ordersUtil.getNumOfOrdersToPay(buyerId);
        int ordersToDeliver = ordersUtil.getNumOfOrdersToDeliver(buyerId);
        int ordersToEvaluate = ordersUtil.getNumOfOrdersToEvaluate(buyerId);
        int ordersToService = ordersUtil.getNumOfOrdersToService(buyerId);
        Map<String, Integer> result = new HashMap<>();
        result.put("ordersToPay", ordersToPay);
        result.put("ordersToDeliver", ordersToDeliver);
        result.put("ordersToEvaluate", ordersToEvaluate);
        result.put("ordersToService", ordersToService);
        return new ResponseBean(HttpStatus.OK.value(), "查询成功", result);
    }
}
